package com.yy.finvideoplayer.media

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkMediaPlayer

/**
 * @Description: IjkMedia
 * @Author: yiw89
 * @CreateDate: 2025/9/1 14:02
 * @Version: 1.0
 */
class IjkMedia(iMediaCallback: IMediaCallback) :
    IjkBaseMedia(iMediaCallback) {
    @Throws(Exception::class)
    override fun getMedia(
        context: Context,
        url: String,
        headers: Map<String, String?>?,
        vararg objects: Any?
    ): IMediaPlayer {
        val mediaPlayer = IjkMediaPlayer()

        if (url.startsWith(ContentResolver.SCHEME_CONTENT) || url.startsWith(ContentResolver.SCHEME_ANDROID_RESOURCE))
            mediaPlayer.setDataSource(context,Uri.parse(url),headers)
        else mediaPlayer.setDataSource(url, headers)


        if (objects != null && objects.isNotEmpty() && objects[0] is List<*>) {
            val list = objects[0] as List<Option>
            for (o in list) {
                if (o.strValue != null) mediaPlayer.setOption(o.category, o.name, o.strValue)
                else mediaPlayer.setOption(o.category, o.name, o.longValue)
            }
        }

        //mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 1);

//        if (mSettings.getUsingMediaCodec()) {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
//            if (mSettings.getUsingMediaCodecAutoRotate()) {
//                mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
//            } else {
//                mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 0);
//            }
//        } else {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 0);
//        }
//
//        if (mSettings.getUsingOpenSLES()) {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 1);
//        } else {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 0);
//        }
//
//        String pixelFormat = mSettings.getPixelFormat();
//        if (TextUtils.isEmpty(pixelFormat)) {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", IjkMediaPlayer.SDL_FCC_RV32);
//        } else {
//            mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", pixelFormat);
//        }
//        mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1);
//        mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
//
//        mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "http-detect-range-support", 0);
//
//        mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
        return mediaPlayer
    }


    override fun setSpeed(rate: Float): Boolean {
        if (isPrepar and (mediaPlayer != null)) {
            (mediaPlayer as IjkMediaPlayer).setSpeed(rate)
        }
        return true
    }

    class Option {
        var category: Int
        var name: String
        var strValue: String? = null
        var longValue: Long = 0

        constructor(category: Int, name: String, value: String?) {
            this.category = category
            this.name = name
            this.strValue = value
        }

        constructor(category: Int, name: String, value: Long) {
            this.category = category
            this.name = name
            this.longValue = value
        }
    }
}